package com.jeecms.wicool.dao.cms.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.cms.CmsSpecialInfoDao;
import com.jeecms.wicool.entity.cms.CmsSpecialInfo;

@Repository
public class CmsSpecialInfoDaoImpl extends HibernateBaseDao<CmsSpecialInfo, Integer> implements
CmsSpecialInfoDao {

	@Override
	protected Class<CmsSpecialInfo> getEntityClass() {
		// TODO Auto-generated method stub
		return CmsSpecialInfo.class;
	}

	@Override
	public Pagination getPage(String title,
			 int pageNo, int pageSize, CmsUser user) {
		Finder f = Finder.create("select bean from CmsSpecialInfo bean where 1=1 ");
		if (!StringUtils.isBlank(title)) {
			f.append(" and bean.title = :title");
			f.setParam("title", title);
		}if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.orderNum desc");
		return find(f, pageNo, pageSize);
	}

	@Override
	public CmsSpecialInfo findById(Integer id) {
		CmsSpecialInfo entity = get(id);
		return entity;
	}

	@Override
	public CmsSpecialInfo save(CmsSpecialInfo bean) {
		getSession().save(bean);
		return bean;
	}

	@Override
	public CmsSpecialInfo deleteById(Integer id) {
		CmsSpecialInfo entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	@Override
	public boolean isHave(CmsSpecialInfo bean){
		Finder f = Finder
		.create("select bean from CmsSpecialInfo bean where 1 = 1 ");
		if(!StringUtils.isBlank(bean.getCmsPath())){
			f.append(" and bean.cmsPath = :cmsPath");
			f.setParam("cmsPath",bean.getCmsPath());
		}if(bean.getShop().getId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",bean.getShop().getId());
		}
		List<CmsSpecialInfo> list=find(f);
		if(list==null||list.size()<=0){
			return false;
		}
		return true;
	}
	@Override
	public CmsSpecialInfo findByCmsSpecialInfo(CmsSpecialInfo bean) {
		Finder f = Finder
				.create("select bean from CmsSpecialInfo bean where 1 = 1 ");
		if(bean.getShop().getId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",bean.getShop().getId());
		}if(!StringUtils.isBlank(bean.getCmsPath())){
			f.append(" and bean.cmsPath = :cmsPath");
			f.setParam("cmsPath",bean.getCmsPath());
		}
		List<CmsSpecialInfo> list= find(f);
		if(list!=null){
			return list.get(0);
		}else{
			return null;
		}
	}
	@Override
	public List<CmsSpecialInfo> getAll(CmsUser user) {
		Finder f = Finder
				.create("select bean from CmsSpecialInfo bean where 1 = 1 ");
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.id desc");
		return find(f);
	}
	@Override
	public CmsSpecialInfo findByContentId(Integer contentId) {
		Finder f = Finder
				.create("select bean from CmsSpecialInfo bean where 1 = 1 ");
		if(contentId!=null){
			f.append(" and bean.contentId = :contentId");
			f.setParam("contentId",contentId);
		}
		List<CmsSpecialInfo> list= find(f);
		if(list!=null&&list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
}